//
//  TJModifyNameController.m
//  BaoDian
//
//  Created by mac on 2024/7/23.
//

#import "TJModifyNameController.h"
#import "QMUITextField.h"
#import "TJButton.h"

@interface TJModifyNameController ()

@property (nonatomic, weak) QMUITextField *textField;
@property (nonatomic, weak) TJButton *confirmButton;

@end

@implementation TJModifyNameController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"修改名称";

    UIView *textFieldContentView = [UIView addToView:self.view];
    textFieldContentView.addCornerRadius(12);
    textFieldContentView.backgroundColor = UIColor.tj_1E1F23Color;
    [textFieldContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.inset(16);
        make.right.inset(88);
        make.height.offset(46);
        make.top.offset(KNavBarHight + 20);
    }];
    
    UILabel *titleLabel = [UILabel lableWithSuperView:textFieldContentView font:UIFontMake(14) color:UIColor.tj_868391Color title:nil textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(20);
    }];

    QMUITextField *textField = [QMUITextField addToView:textFieldContentView];
    textField.font = UIFontMake(14);
    textField.placeholder = @"请输入名称";
    textField.tintColor = UIColor.tj_cursorColor;
    textField.returnKeyType = UIReturnKeyDone;
    textField.textColor = UIColor.whiteColor;
    textField.text = _text;
    [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    textField.placeholderColor = UIColor.tj_placeholderTextColor;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(12);
        make.top.bottom.offset(0);
    }];
    
    TJButton *confirmButton = [TJButton buttonWithSuperView:self.view font:UIFontMediumMake(14) color:UIColor.whiteColor title:@"确认"];
    confirmButton.addCornerRadius(12);
    confirmButton.backgroundColor = UIColorMake(0x2978F0);
    confirmButton.enabled = NO;
    [confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(16);
        make.height.offset(44);
        make.width.offset(60);
        make.centerY.equalTo(textFieldContentView);
    }];
    
    _textField = textField;
    _confirmButton = confirmButton;
    
    [self textFieldDidChange:textField];
}

- (void)textFieldDidChange:(UITextField *)textField {
    _confirmButton.enabled = !isEmptyString(textField.text);
}
 
- (void)didClickConfirmButton {
    [TJHUD showLoadingInView:self.view];
    [TJMinePresent modifyName:_textField.text complete:^(NSError * _Nullable error) {
        if (error) {
            return;
        }
        
        TJUserModel *userModel = [TJUserModel currentAccountModel];
        userModel.realName = self.textField.text;
        [userModel bg_saveOrUpdateAsync:^(BOOL isSuccess) {
            
            dispatch_async_main_queue(^{
                if (self.completeBlock) {
                    self.completeBlock(self.textField.text);
                }
            });
        }];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}


@end
